#!/bin/bash
# postinst script for jicofo

set -e

case "$1" in
    configure)

        CONFIG="/etc/jitsi/jicofo/config"

        # we don't want to regenerate config on upgrade
        OLDCONFIG="false"
        if [ -f $CONFIG ]; then
            . $CONFIG
            if [ -n "$JICOFO_HOSTNAME" ] && [ -n "$JICOFO_PORT" ] && [ -n "$JICOFO_SECRET" ] \
                && [ -n "$JICOFO_AUTH_DOMAIN" ] && [ -n "$JICOFO_AUTH_USER" ] && [ -n "$JICOFO_AUTH_PASSWORD" ]; then
                OLDCONFIG="true"
            fi
        fi

        JICOFO_JAVA_SYS_PROPS_EXIST="true"
        # let's check whether there is a setting in the $CONFIG
        # for home folder and logging props file
        if [ ! -n "$JAVA_SYS_PROPS" ]; then
            JICOFO_JAVA_SYS_PROPS_EXIST="false"
        fi

        # generate config on new install
        if [ "$OLDCONFIG" = "false" ]; then

            # try to get host from jitsi-videobridge
            . /etc/jitsi/videobridge/config

            JICOFO_HOSTNAME="$JVB_HOSTNAME"
            # 8-chars random secret, alternative to pwgen 8
            JICOFO_SECRET=`head -c 8 /dev/urandom | tr '\0-\377' 'a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9@@@@####'`
            JICOFO_PORT=5347
            JICOFO_AUTH_DOMAIN="auth.$JICOFO_HOSTNAME"
            JICOFO_AUTH_USER="focus"
            JICOFO_AUTH_PASSWORD=`head -c 8 /dev/urandom | tr '\0-\377' 'a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9@@@@####'`

            # storing default
            echo '# Jitsi Conference Focus settings' > $CONFIG
            echo '# sets the host name of the XMPP server' >> $CONFIG
            echo "JICOFO_HOST=localhost" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the XMPP domain (default: none)' >> $CONFIG
            echo "JICOFO_HOSTNAME=$JICOFO_HOSTNAME" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the secret used to authenticate as an XMPP component' >> $CONFIG
            echo "JICOFO_SECRET=$JICOFO_SECRET" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the port to use for the XMPP component connection' >> $CONFIG
            echo "JICOFO_PORT=$JICOFO_PORT" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the XMPP domain name to use for XMPP user logins' >> $CONFIG
            echo "JICOFO_AUTH_DOMAIN=$JICOFO_AUTH_DOMAIN" >> $CONFIG
            echo >> $CONFIG 
            echo '# sets the username to use for XMPP user logins' >> $CONFIG
            echo "JICOFO_AUTH_USER=$JICOFO_AUTH_USER" >> $CONFIG
            echo >> $CONFIG
            echo '# sets the password to use for XMPP user logins' >> $CONFIG
            echo "JICOFO_AUTH_PASSWORD=$JICOFO_AUTH_PASSWORD" >> $CONFIG
            echo >> $CONFIG
            echo '# extra options to pass to the jicofo daemon' >> $CONFIG
            echo "JICOFO_OPTS=\"\"" >> $CONFIG

        fi

        OLD_JITSI_CONFIG="/usr/share/jicofo/.sip-communicator/sip-communicator.properties"
        NEW_JITSI_CONFIG="/etc/jitsi/jicofo/sip-communicator.properties"
        if [ -f $OLD_JITSI_CONFIG ]; then
            mv $OLD_JITSI_CONFIG $NEW_JITSI_CONFIG
            echo "# Config has moved to /etc/jitsi/jicofo/
# do not edit this file as it is not used" > ${OLD_JITSI_CONFIG}.old
        elif [ ! -f $NEW_JITSI_CONFIG ]; then
            # if sip-communicator.properties file is missing create it
            # as jicofo will search for it and if cannot create it will fail starting
            touch $NEW_JITSI_CONFIG
        fi

        # let's check whether there is a setting in the $CONFIG
        # for home folder and logging props file, if missing add it
        if [ "$JICOFO_JAVA_SYS_PROPS_EXIST" = "false" ]; then
            echo >> $CONFIG
            echo '# adds java system props that are passed to jicofo (default are for home and logging config file)' >> $CONFIG
            echo "JAVA_SYS_PROPS=\"-Dnet.java.sip.communicator.SC_HOME_DIR_LOCATION=/etc/jitsi\
 -Dnet.java.sip.communicator.SC_HOME_DIR_NAME=jicofo\
 -Dnet.java.sip.communicator.SC_LOG_DIR_LOCATION=/var/log/jitsi\
 -Djava.util.logging.config.file=/etc/jitsi/jicofo/logging.properties\"" >> $CONFIG
        fi

        # we don't want to start the daemon as root
        if ! getent group jitsi > /dev/null ; then
            groupadd jitsi
        fi
        if ! getent passwd jicofo > /dev/null ; then
            useradd -r -g jitsi --shell /bin/bash --create-home -d /usr/share/jicofo jicofo
        fi
        if ! groups jicofo | grep '\bjitsi\b' > /dev/null ; then
            usermod -g jitsi jicofo
        fi

        # we create home folder only if it doesn't exist
        if [ ! -d /usr/share/jicofo ]; then
            mkdir -p /usr/share/jicofo
        fi

        # we claim the home folder of jicofo in case it is owned by someone else
        OWNER=$(stat -c '%U' /usr/share/jicofo)
        GROUP=$(stat -c '%G' /usr/share/jicofo)
        if ! dpkg-statoverride --list /usr/share/jicofo/* >/dev/null && [ "$OWNER:$GROUP" != "jicofo:jitsi" ]; then
            chown -R jicofo:jitsi /usr/share/jicofo
            OWNER=jicofo
            GROUP=jitsi
        fi

        CONFIG_DIR=$(dirname $CONFIG)
        if ! dpkg-statoverride --list "$CONFIG_DIR" >/dev/null; then
                chown root:$GROUP "$CONFIG_DIR"
                chmod 750 "$CONFIG_DIR"
        fi

        # die logz
        ls /var/log/jitsi/jicofo* 1>/dev/null 2>&1 && chown -f -R $OWNER:$GROUP /var/log/jitsi/jicofo*
        ls /var/log/jitsi/jicofo* 1>/dev/null 2>&1 && chmod -f -R 640 /var/log/jitsi/jicofo*

        # ensure focus is not running - it will be started at the end
        if invoke-rc.d jicofo status >/dev/null 2>&1 ; then
            invoke-rc.d jicofo stop || true
        fi

        # clean up old jicofo group
        if getent group jicofo > /dev/null; then
            groupdel jicofo
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
